package com.banciyuan.bcywebview.biz.circles.smooth.adapter;

import android.content.Context;
import android.os.Build;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.TimelineLikeHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.circles.CircleMoreUserActivity;
import com.banciyuan.bcywebview.biz.circles.smooth.fragment.CircleSmoothUserTopFragment;
import com.banciyuan.bcywebview.biz.pc.zone.smooth.SmoothPersonActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.daoexample.model.ActiveUser;


/**
 * Created by squall on 2016/9/30.
 */

public class CircleSmoothTopUserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TITLE = 0;
    private static final int USER = 1;
    private static final int USER_TOP = 3;
    private static final int MORE = 2;

    private Map<String, Integer> map = new HashMap<>();
    private List<ActiveUser> alist;
    private Context mContext;
    private LayoutInflater layoutInflater;
    private String id;

    public CircleSmoothTopUserAdapter(Context mContext, List<ActiveUser> alist, Map<String, Integer> map, String id) {
        this.alist = alist;
        this.map = map;
        this.mContext = mContext;
        this.id = id;
        this.layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TITLE:
                return new ViewTitle(layoutInflater.inflate(R.layout.circle_user_top_header, null));
            case USER:
                return new ViewHolder(layoutInflater.inflate(R.layout.circle_top_user_card, null));
            case USER_TOP:
                return new ViewHolder(layoutInflater.inflate(R.layout.circle_top_user_card_champion, null));
            case MORE:
                return new ViewMore(layoutInflater.inflate(R.layout.circle_user_more, null));
            default:
                return new SmoothBlank(layoutInflater.inflate(R.layout.smooth_blank, null));
        }
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TITLE:
                setTitle((ViewTitle) holder, position);
                break;
            case USER:
                setUser((ViewHolder) holder, position);
                break;
            case USER_TOP:
                setUser((ViewHolder) holder, position);
                break;
            case MORE:
                setMore((ViewMore) holder, position);
                break;
            default:
                SmoothBlank smoothBlank = (SmoothBlank) holder;
                LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
                smoothBlank.blank.setLayoutParams(ll);
                break;
        }
    }

    private void setUser(ViewHolder viewHolder, int position) {
        final ActiveUser activeUser = alist.get(position);
        if (activeUser == null) return;
        if (!TextUtils.isEmpty(activeUser.getAvatar())) {
            ImageLoader.getInstance().displayImage(activeUser.getAvatar(), viewHolder.head_iv, ImageOptions.optionHead);
        }
        if (activeUser.getIndex() == 1) {
            viewHolder.head_iv.setBorderColor(mContext.getResources().getColorStateList(R.color.top_two_red));
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                viewHolder.mark.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_two_back, mContext.getTheme()));
            } else {
                viewHolder.mark.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_two_back));
            }
        } else if (activeUser.getIndex() == 2) {
            viewHolder.head_iv.setBorderColor(mContext.getResources().getColorStateList(R.color.top_three_yellow));
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
                viewHolder.mark.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_three_back, mContext.getTheme()));
            } else {
                viewHolder.mark.setImageDrawable(mContext.getResources().getDrawable(R.drawable.top_three_back));
            }
        }
        if (!TextUtils.isEmpty(activeUser.getUname())) {
            viewHolder.uname_tv.setText(activeUser.getUname());
        }
        if (!TextUtils.isEmpty(activeUser.getFollower())) {
            viewHolder.fansCount_tv.setText(activeUser.getFollower());
        }
        if (!TextUtils.isEmpty(activeUser.getPost_count())) {
            viewHolder.production_tv.setText(activeUser.getPost_count());
        }
        List<ActiveUser.UserPost> ulist = activeUser.getPosts();
        if (ulist != null && !ulist.isEmpty()) {
            for (int i = 0; i < ulist.size(); i++) {
                ActiveUser.UserPost u = ulist.get(i);
                if (i < viewHolder.pics.length) {
                    if (!TextUtils.isEmpty(u.getCover())) {
                        ImageLoader.getInstance().displayImage(u.getCover(), viewHolder.pics[i], ImageOptions.options);
                    }
                    if (!TextUtils.isEmpty(u.getTitle())) {
                        viewHolder.containers[i].setVisibility(View.VISIBLE);
                        viewHolder.titles[i].setText(Html.fromHtml(u.getTitle()));
                    } else {
                        viewHolder.containers[i].setVisibility(View.GONE);
                    }
                    if (!TextUtils.isEmpty(u.getPlain())) {
                        viewHolder.plains[i].setText(Html.fromHtml(u.getPlain()));
                    }
                }
                final int finalI = i;
                viewHolder.pics[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActiveUser.UserPost userPost = activeUser.getPosts().get(finalI);
                        if (activeUser.getType().equals(CircleSmoothUserTopFragment.COSER)) {
                            DetailDataHelper.gotoCosDetail(mContext, userPost.getRp_id(), userPost.getCp_id(), false);
                        } else if (activeUser.getType().equals(CircleSmoothUserTopFragment.DRAWER)) {
                            DetailDataHelper.gotoDrawerDetail(mContext, userPost.getRp_id(), userPost.getDp_id(), false);
                        } else if (activeUser.getType().equals(CircleSmoothUserTopFragment.WRITER)) {
                            DetailDataHelper.gotoWriterDetail(mContext, userPost.getRp_id(), userPost.getWp_id(), false);
                        }
                    }
                });
            }
        }
        if (activeUser.getFollowstate() != null && activeUser.getFollowstate().equals("unfollow") && !StringUtil.notNullEqual(activeUser.getUid(), UserDataHelper.getInstance(mContext).getUid())) {
            viewHolder.foucs_tv.setSelected(false);
            final ViewHolder finalViewHolder = viewHolder;
            viewHolder.foucs_tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!v.isSelected()) {
                        TimelineLikeHelper.doFollow(mContext, "dofollow", activeUser.getUid(), new TimelineLikeHelper.FollowSuccess() {
                            @Override
                            public void onFollowSuccess(String response) {
                                MyToast.show(mContext, mContext.getString(R.string.focus_succ));
                                finalViewHolder.foucs_tv.setSelected(true);
                                activeUser.setFollowstate("havafollow");
                            }
                        });
                    }
                }
            });
        } else {
            viewHolder.foucs_tv.setVisibility(View.GONE);
        }
        View.OnClickListener goPc = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoUtil.gotoAct(mContext, SmoothPersonActivity.class, activeUser.getUid());
            }
        };
        viewHolder.head_iv.setOnClickListener(goPc);
        viewHolder.uname_tv.setOnClickListener(goPc);
    }

    private void setTitle(ViewTitle viewTitle, int position) {
        ActiveUser activeUser = alist.get(position);
        if (activeUser.getType().equals(CircleSmoothUserTopFragment.COSERTITLE)) {
            viewTitle.title.setText(mContext.getString(R.string.high_coser));
            if (map.get(CircleSmoothUserTopFragment.COSER) == 0) {
                viewTitle.empty.setVisibility(View.VISIBLE);
            } else {
                viewTitle.empty.setVisibility(View.GONE);
            }
        } else if (activeUser.getType().equals(CircleSmoothUserTopFragment.DRAWERTITLE)) {
            viewTitle.title.setText(mContext.getString(R.string.high_drawer));
            if (map.get(CircleSmoothUserTopFragment.DRAWER) == 0) {
                viewTitle.empty.setVisibility(View.VISIBLE);
            } else {
                viewTitle.empty.setVisibility(View.GONE);
            }
        } else {
            viewTitle.title.setText(mContext.getString(R.string.high_writer));
            if (map.get(CircleSmoothUserTopFragment.WRITER) == 0) {
                viewTitle.empty.setVisibility(View.VISIBLE);
            } else {
                viewTitle.empty.setVisibility(View.GONE);
            }
        }
    }

    private void setMore(ViewMore viewMore, int position) {
        final ActiveUser activeUser = alist.get(position);
        viewMore.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (activeUser.getType().contains(CircleSmoothUserTopFragment.COSER)) {
                    gotoUtil.gotoAct(mContext, CircleMoreUserActivity.class, id, CircleSmoothUserTopFragment.COSER);
                } else if (activeUser.getType().contains(CircleSmoothUserTopFragment.WRITER)) {
                    gotoUtil.gotoAct(mContext, CircleMoreUserActivity.class, id, CircleSmoothUserTopFragment.WRITER);
                } else {
                    gotoUtil.gotoAct(mContext, CircleMoreUserActivity.class, id, CircleSmoothUserTopFragment.DRAWER);
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return alist.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (alist.get(position).getType().contains("title")) {
            return TITLE;
        } else if (alist.get(position).getType().contains("more")) {
            return MORE;
        } else {
            if (alist.get(position).getIndex() == 0) {
                return USER_TOP;
            } else {
                return USER;
            }
        }
    }

    private class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView mark;
        private RoundedImageView head_iv;
        private TextView uname_tv;
        private TextView production_tv;
        private TextView fansCount_tv;
        private TextView foucs_tv;
        private ImageView[] pics;
        private TextView[] plains;
        private TextView[] titles;
        private LinearLayout[] containers;

        public ViewHolder(View convertView) {
            super(convertView);
            mark = (ImageView) convertView.findViewById(R.id.rank_mark);
            head_iv = (RoundedImageView) convertView.findViewById(R.id.user_head);
            uname_tv = (TextView) convertView.findViewById(R.id.user_name);
            production_tv = (TextView) convertView.findViewById(R.id.production_tv);
            fansCount_tv = (TextView) convertView.findViewById(R.id.fanscount_tv);
            foucs_tv = (TextView) convertView.findViewById(R.id.user_focus);
            pics = new ImageView[]{(ImageView) convertView.findViewById(R.id.pic_one),
                    (ImageView) convertView.findViewById(R.id.pic_two),
                    (ImageView) convertView.findViewById(R.id.pic_three)};
            plains = new TextView[]{(TextView) convertView.findViewById(R.id.plain_one),
                    (TextView) convertView.findViewById(R.id.plain_two),
                    (TextView) convertView.findViewById(R.id.plain_three)};

            titles = new TextView[]{(TextView) convertView.findViewById(R.id.title_one),
                    (TextView) convertView.findViewById(R.id.title_two),
                    (TextView) convertView.findViewById(R.id.title_three)};

            containers = new LinearLayout[]{(LinearLayout) convertView.findViewById(R.id.tv_container_one),
                    (LinearLayout) convertView.findViewById(R.id.tv_container_two),
                    (LinearLayout) convertView.findViewById(R.id.tv_container_three)};
        }
    }

    private class ViewTitle extends RecyclerView.ViewHolder {
        private TextView title;
        private LinearLayout empty;

        public ViewTitle(View convertView) {
            super(convertView);
            title = (TextView) convertView.findViewById(R.id.label_title);
            empty = (LinearLayout) convertView.findViewById(R.id.empty_view);
        }
    }

    private class ViewMore extends RecyclerView.ViewHolder {

        private TextView more;

        public ViewMore(View itemView) {
            super(itemView);
            more = (TextView) itemView.findViewById(R.id.find_more);
        }
    }

    private class SmoothBlank extends RecyclerView.ViewHolder {

        private View blank;

        public SmoothBlank(View itemView) {
            super(itemView);
            blank = itemView.findViewById(R.id.blank);
        }
    }
}
